<?php
class Customer_model extends CI_Model {
	public function __construct() {
		parent :: __construct();
	}

	public function count_nums($name = ''){ /*取得总行数*/
			$user_id = $this->session->userdata('id');
			$role = $this->session->userdata('role');
			$username = $this->session->userdata('username');
			$count = 0;$id_array = array();$parent_id_array = array();
			if($role == '市场'){
				$customers = array();
				$parents = '';
				$sql = "SELECT * FROM customer WHERE 1=1 ";
				if ('经理' <> $role && '商务助理' <> $role) {
					$sql .= " AND user_id = {$user_id}";
					$sql .= " OR INSTR(`share`, '{$username}')";
				}
				$query = $this->db->query($sql);
				$customers = $query->result();
				
				$last_parent_id = 0;$last_id = 0;
				for($i=0;$i<count($customers);$i++){ /*$customers 所有子级客户*/
					$parent_id = $customers[$i]->parent_id;
					$id = $customers[$i]->id; /*子级客户ID*/
					if($parent_id && ($parent_id != $last_parent_id)){
						$id = $customers[$i]->parent_id;
						$sql = "select customer.*,user.user_name as user_name from customer inner join user on customer.user_id = user.id where customer.id = $id";
						$county_query = $this->db->query($sql);
						$parents[] = $county_query->result(); /*获取父级客户*/
					}
					$last_parent_id = $parent_id;
				}
				$count = count($parents);
			}else{
				$count = $this->db->where('parent_id',0)->count_all_results('customer');
			}
			return $count;
	}

	public function list_customers($limit, $offset = 0,$name = ''){ /*获取分页customer数据*/
		$user_id = $this->session->userdata('id');
		$role = $this->session->userdata('role');
		$username = $this->session->userdata('username');

		$customers = array();
		$parents = '';
		$sql = "SELECT * FROM customer WHERE 1=1 "; 
		if('经理' == $role || '商务助理' == $role){
			$sql .= "and parent_id = 0";
		}
		
		if ('经理' <> $role && '商务助理' <> $role) {
			$sql .= " AND user_id = {$user_id}";
			$sql .= " OR INSTR(`share`, '{$username}')";
		}
		$sql .= " ORDER BY id desc";
		$sql .= " LIMIT {$offset}, {$limit}";
		
		$query = $this->db->query($sql);
		
		foreach ($query->result() as $row) {
			$user_query = $this->db->get_where ( 'user', array ('id' => $row->user_id ) );
			$user = $user_query->row();
			$row->user_name = $user->user_name;
			
			$customers[] = $row;
		}
		if('经理' == $role || '商务助理' == $role){
			for($i=0;$i<count($customers);$i++){
				$parent_id = $customers[$i]->id;
				$sql = "select customer.*,user.user_name as user_name from customer inner join user on customer.user_id = user.id where parent_id = $parent_id";
				$county_query = $this->db->query($sql);
				$counties = $county_query->result();
				$customers[$i]->counties = $counties;
				if(!count($customers[$i]->counties)){
					$customers[$i]->cus_name_list = 'none';
				}
			}
			return $customers; 
		}else{
			$data_array = $this->get_market_customers($customers);
			return $data_array;
		}
	}
	
	public function get_market_customers($customers = ''){ /*市场人员登录时获取数据*/
		$cu_count = count($customers);
		$user_id = $this->session->userdata('id');
		$user_name = $this->session->userdata('username');
		$last_parent_id = 0;$last_id = 0;
		$parent_id_array = array();
		for($i=0;$i<$cu_count;$i++){ /*$customers 所有子级客户*/
			$parent_id = $customers[$i]->parent_id;
			$id = $customers[$i]->id; /*子级客户ID*/
			$parent_id_array[] = $parent_id;
		}
		$count_parent_id_array = count($parent_id_array);
		$data_unique = array_unique($parent_id_array);
		for($i=0;$i<$count_parent_id_array;$i++){
			$id = $data_unique[$i];
			if($id){
				$sql = "select customer.*,user.user_name as user_name from customer inner join user on customer.user_id = user.id where customer.id = $id";
				$county_query = $this->db->query($sql);
				$parents[] = $county_query->result(); /*获取父级客户*/
			}
		}
		if($parents){
			$data_array = array();
			for($p=0;$p<count($parents);$p++){
				$data_array[$p] = $parents[$p][0];
			}
			for($t=0;$t<count($data_array);$t++){
				for($o=0;$o<$cu_count;$o++){
					if($customers[$o]->customer_name == $data_array[$t]->customer_name){
							unset($customers[$o]);
					}
				}
			}
			for($m=0;$m<count($data_array);$m++){
				if($data_array[$m]->id){
					$par_id = $data_array[$m]->id;
					$sql = "select customer.*,user.user_name as user_name from customer inner join user on customer.user_id = user.id where customer.parent_id = $par_id and (customer.user_id = $user_id OR INSTR(`share`, '{$user_name}'))";
					$counties_query = $this->db->query($sql);
					$counties = $counties_query->result();
					$data_array[$m]->counties = $counties;
				}else{
					$data_array[$m] = array();
				}
			}
			for($m=0;$m<count($data_array);$m++){
				if(!$data_array[$m]){
					unset($data_array[$m]);
				}
			}
			for($r=0;$r<$cu_count;$r++){ /*添加没有子级客户的父级客户*/
				if($customers[$r]){
					if(!$customers[$r]->parent_id){
						$customers[$r]->cus_name_list = 'none';
						$data_array[] = $customers[$r];
					}
				}
			}
		}else{
			for($y=0;$y<count($customers);$y++){
				$customers[$y]->cus_name_list = 'none';
			}
			$data_array = $customers;
		}
		return $data_array;
	}
}
?>